/*
 * DBConnection.java
 *
 * Created on 30. August 2007, 13:43
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.googlecode.oskis.library.db;
import java.sql.*;
import java.util.Properties;
import java.io.FileInputStream;

/**
 * Abstrakte Basisklasse für Datenbankverbindungen
 * <br>Abgeleitete Klassen z.B. für eine Oracle Verbindung müssen<br>
 * die Methode createConnection() implementieren;
 * Diese Basisklasse implementiert übergreifende Informationen,<br>
 * wie Name der Datenbank,Username, Passwort etc.<br>

 * @author thomas
 */
public abstract class DBConnection {

    protected Connection db;
    protected String user=null;
    protected String passwd=null;
    protected String url=null;
    protected String ip=null;
    protected String port=null;
    protected String database=null;    
    
    private static final String defaultPropertiesFile="dbconnection.conf";
    private static String applicationWorkingDirectory;
    
    private Properties props;
    
    protected boolean connected=false;
    protected String connectionError=null;
    
    
    /** Abstract Method to create the Connection differs for each Database 
        Classes implementing this method need to return a java.sql.Connection Object
        pointing to the Database
    */
    protected abstract Connection createConnection();
    
    
    /** Diese Methode öffnet die in 'propertiesFile' übergebene Datei<br>
     * und liest daraus die Konfigurationsparameter
     * user,passwd,url,ip,port,database<br>
     * Angabe der Konfigurationsparameter in der form <br>
     * database=odkisdb
     *   
     */
    protected void readProperties(String propertiesFile) {
        try {
            FileInputStream fis=new FileInputStream(applicationWorkingDirectory+"/"+propertiesFile);
            props.load(fis);
            user=props.getProperty("user");
            passwd=props.getProperty("passwd");
            url=props.getProperty("url");
            ip=props.getProperty("ip");
            port=props.getProperty("port");
            database=props.getProperty("database");
        } catch(java.io.IOException e) {
            System.out.println(e);
            user=passwd=url=ip=port=database=null;
        }        
    }
    
    
    /** Liest Konfigurationsdaten aus der Default Datei<br>
        dbconnection.conf
     */
    protected void readProperties() {
        this.readProperties(applicationWorkingDirectory+"/"+defaultPropertiesFile);
    }
    
    
    /** Setzt das Standardverzeichnis, in dem sich die <br>
     *  Konfigurationsdatei dbconnection.conf befindet<br>
     *  (default=Verzeichnis, in dem die Applikation ausgeführt wird)<br>
     * @param dir
     */
    public void setApplicationWorkingDirectory(String dir) {
        applicationWorkingDirectory=dir;
    }
    
    /** Liefert das Verzeichnis, in dem sich die Konfigurationsdatei befindet
     * 
     * @return
     */
    public String getApplicationWorkingDirectory() {
        return(applicationWorkingDirectory);
    }
    
    /**
     * Liefert true, wenn die Verbindung hergestellt ist
     * @return
     */
    public boolean isConnected() {
        return(connected);
    }
    /**
     * liefert im Fehlerfall die Fehlermeldung der Datenbank
     * @return
     */
    public String getConnectionError() {
        return(connectionError);
    }
    
    /** Set Username */
    public void setUser(String user) {
        this.user=user;
    }
    
    /** Set Password */
    public void setPasswd(String passwd) {
        this.passwd=passwd;
    }
    
    /** Set IP Address */
    public void setIP(String ip) {
        this.ip=ip;
    }
    
    /** Set Port */
    public void setPort(String port) {
        this.port=port;
    }
    
    /** Set Database Name */
    public void setDatabase(String database) {
        this.database=database;
    }
    
    /** returns Connection to the Database */
    public Connection getConnection() {
        return(db);
    }
    
    /** returns true if Username is Set */
    public boolean userIsSet() {
        return(user!=null);
    }
    /** returns true if Password is Set */
    public boolean passwdIsSet() {
        return(passwd!=null);
    }
    /** returns true if IP is Set */
    public boolean ipIsSet() {
        return(ip!=null);
    }
    /** returns true if Port is Set */
    public boolean portIsSet() {
        return(port!=null);
    }
    /** returns true if database is Set */
    public boolean databaseIsSet() {
        return(database!=null);
    }
    
    /** Creates a new instance of DBConnection */
    public DBConnection() {
        applicationWorkingDirectory=System.getProperty("user.dir");
    }
    
}
